package com.hymns;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xml.sax.SAXException;

import android.content.res.Resources.NotFoundException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hymns.domain.Hymn;
import com.hymns.utils.XmlParser;

public class XmlAdapter extends BaseAdapter {

	private HymnsActivity _hymnsActivity;
	private ArrayList<Hymn> _hymns;

	public XmlAdapter(HymnsActivity hymnsActivity) {

		_hymnsActivity = hymnsActivity;

		try {

			InputStream stream = hymnsActivity.getResources().openRawResource(
					R.raw.summary);

			_hymns =  XmlParser.getSingletonParser().getHymns(stream);

		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int getCount() {
		return this._hymns.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		View MyView = convertView;

		if (convertView == null) {
			LayoutInflater li = _hymnsActivity.getLayoutInflater();

			MyView = li.inflate(R.layout.grid_item, null);

			Hymn currentHymn = _hymns.get(position);

			//TODO DELETE 
			Log.d("position", "" + position);
			
			TextView tv = (TextView) MyView.findViewById(R.id.grid_item_text);

			tv.setText(currentHymn.get_FormatedTitle());

			ImageView iv = (ImageView) MyView
					.findViewById(R.id.grid_item_image);

			iv.setImageResource(R.drawable.ic_play);

			MyView.setTag(currentHymn);
		}

		return MyView;
	}

}
